//
//  SingleGoodsViewController.m
//  薄荷家具
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "SingleGoodsViewController.h"
#import "SingleCollectionViewCell.h"
#import "ZHLNewWorking.h"
#import "SingleGoods.h"
#import "MJRefresh.h"
@interface SingleGoodsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,retain)UICollectionView *bigCollection;


@property(nonatomic,retain)NSMutableArray *collectionArray;

@property(nonatomic,assign)NSInteger currentPage;


@end

@implementation SingleGoodsViewController
-(void)dealloc
{
    [_bigCollection release];
    [_collectionArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.361 green:0.847 blue:0.710 alpha:1];
    self.navigationItem.title = @"热门";
    
#pragma mark 初始化
    
    self.collectionArray = [NSMutableArray array];
    
#pragma mark 创建单品collection
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.bigCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:layout];
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    self.bigCollection.backgroundColor = [UIColor grayColor];
    layout.minimumInteritemSpacing = 5*sHeight;
    
    layout.minimumLineSpacing = 15*sHeight;
    
#pragma mark 注册一个cell
    [self.bigCollection registerClass:[SingleCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    [self.view addSubview:self.bigCollection];
    [_bigCollection release];
    [layout release];
    
#pragma mark 解析数据
    [ZHLNewWorking GetDataWithURL:@"http://api.liwushuo.com/v2/items?gender=1&generation=4&limit=50&oddset=0" dic:nil sussess:^(id responseObject) {
        
        NSArray *array = responseObject[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            NSDictionary *Dic = dic[@"data"];
            SingleGoods *single = [[SingleGoods alloc]init];
            [single setValuesForKeysWithDictionary:Dic];
            [self.collectionArray addObject:single];
            
            
        }
        NSLog(@"%@",self.collectionArray);
        [self.bigCollection reloadData];
    } filed:^(NSError *error) {
        
    }];
    
    
    [self addfooter];
    
    
}


-(void)addfooter
{
    __block SingleGoodsViewController *VC = self;
    
    [self.bigCollection addFooterWithCallback:^{
        VC.currentPage += 50;
       
        
        [self getModel:VC.currentPage];
        
    }];
    
}


-(void)getModel:(NSInteger)nextPage
{
    [ZHLNewWorking GetDataWithURL:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items?generation=4&gender=1&oddset=0&limit=50&offset=%ld",self.currentPage] dic:nil sussess:^(id responseObject) {
        
        NSArray *array = responseObject[@"data"][@"items"];
        for (NSDictionary *dic in array) {
            NSDictionary *Dic = dic[@"data"];
            SingleGoods *single = [[SingleGoods alloc]init];
            [single setValuesForKeysWithDictionary:Dic];
            [self.collectionArray addObject:single];
            
            
        }
        NSLog(@"%@",self.collectionArray);
        [self.bigCollection reloadData];
        [self.bigCollection footerEndRefreshing];
     
    } filed:^(NSError *error) {
        
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SingleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    SingleGoods *single = self.collectionArray[indexPath.item];
    
    cell.single = single;
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kWidth - 30 * sWidth) / 2, 250 * sHeight);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 *sHeight, 10*sHeight, 10*sHeight, 10*sHeight);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
